package model;

import java.util.ArrayList;

/**
 * 
 * @author Christian Gartner nielsen
 */
public class ItemGroupCollection {

	private static ItemGroupCollection instance;

	private ArrayList<ItemGroup> itemGroups;

	private ItemGroupCollection() {
		itemGroups = new ArrayList<ItemGroup>();
	}

	public static ItemGroupCollection getInstance() {
		if (instance == null) {
			instance = new ItemGroupCollection();
		}

		return instance;
	}

	/**
	 * Adds item to collection.
	 * 
	 * @param item
	 */
	public void addItemGroup(ItemGroup itemGroup) {
		itemGroups.add(itemGroup);
	}

	/**
	 * Returns array of all items.
	 * 
	 * @return non-referrence array of all items.
	 */
	public ItemGroup[] getItemGroups() {
		return itemGroups.toArray(new ItemGroup[itemGroups.size()]);
	}

	public boolean hasItemGroup(ItemGroup itemGroup) {
		return itemGroups.contains(itemGroup);
	}

	/**
	 * Removes item from collection.
	 * 
	 * @param item
	 *            object to remove.
	 */
	public void removeItemGroup(ItemGroup itemGroup) {
		itemGroups.remove(itemGroup);
	}

	/**
	 * Returns item from collection by itemNo comparison.
	 * 
	 * @param itemNo
	 *            itemNo to match against.
	 * @return Matching item or null if none.
	 */
	public ItemGroup getItemGroup(String name) {
		boolean found = false;
		int index = 0;
		ItemGroup currentItemGroup = null;
		while (!found && index < itemGroups.size()) {
			currentItemGroup = itemGroups.get(index);
			if (currentItemGroup.getName().equals(name)) {
				found = true;
			}
			index++;
		}
		return currentItemGroup;
	}

}